package Demo06;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;

public class hi extends JFrame{
    public hi()
    {
        super("简易文本编辑器");
        //创建菜单栏(JMenuBar)对象
        JMenuBar mBar = new JMenuBar();
        //在JFrame等容器中设置菜单栏对象，即将菜单栏添加到框架容器中
        this.setJMenuBar(mBar);
        //创建菜单对象
        JMenu file = new JMenu("文件");
        JMenu edit = new JMenu("编辑");
        //将菜单添加到菜单栏中
        mBar.add(file);
        mBar.add(edit);
        JTextArea workArea = new JTextArea();
        JScrollPane imgScrollPane = new JScrollPane(workArea);
        //add(workArea);
        add(imgScrollPane,BorderLayout.CENTER);
        //定义打开和保存对话框
        FileDialog openDia;
        FileDialog saveDia;
        //默认模式为 FileDialog.LOAD
        openDia = new FileDialog(this,"打开(O)",FileDialog.LOAD);
        saveDia = new FileDialog(this,"另存为(A)",FileDialog.SAVE);
        JMenuItem item1_1 = new JMenuItem("新建(N)");
        item1_1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                workArea.setText("");//清空文本
            }
        });
        JMenuItem item1_2 = new JMenuItem("打开(O)");
        item1_2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                openDia.setVisible(true);

                String dirPath = openDia.getDirectory();//获取文件路径
                String fileName = openDia.getFile();//获取文件名称
                //如果打开路径 或 目录为空 则返回空
                if(dirPath == null || fileName == null){
                    return;
                }
                workArea.setText("");//清空文本
                File fileO = new File(dirPath,fileName);
                try{
                    BufferedReader bufr = new BufferedReader(new FileReader(fileO));

                    String line = null;

                    while((line = bufr.readLine()) != null){
                        workArea.append(line + "\r\n");
                    }

                    bufr.close(); //关闭文本
                }catch(IOException er1){
                    throw new RuntimeException("文件读取失败！");
                }

            }
        });
        JMenuItem item1_3 = new JMenuItem("保存(S)");
        item1_3.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                File fileS = null;
                if(fileS == null){
                    saveDia.setVisible(true);
                    String dirPath = saveDia.getDirectory();
                    String fileName = saveDia.getFile();

                    if(dirPath == null || fileName == null)
                        return;

                    fileS = new File(dirPath,fileName);
                }

                try{
                    BufferedWriter bufw = new BufferedWriter(new FileWriter(fileS));
                    String text = workArea.getText();
                    bufw.write(text);
                    bufw.close();
                }catch(IOException er){
                    throw new RuntimeException("文件保存失败！");
                }
            }
        });
        JMenuItem item1_4 = new JMenuItem("另存为(A)");
        item1_4.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                File fileS = null;
                if(fileS == null){
                    saveDia.setVisible(true);
                    String dirPath = saveDia.getDirectory();
                    String fileName = saveDia.getFile();

                    if(dirPath == null || fileName == null)
                        return;

                    fileS = new File(dirPath,fileName);
                }

                try{
                    BufferedWriter bufw = new BufferedWriter(new FileWriter(fileS));
                    String text = workArea.getText();
                    bufw.write(text);
                    bufw.close();
                }catch(IOException er){
                    throw new RuntimeException("文件保存失败！");
                }
            }
        });
        JMenuItem item1_5 = new JMenuItem("退出(Q)");
        item1_5.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                System.exit(0);
            }
        });

        JMenuItem item2_1 = new JMenuItem("剪切(X)");
        JMenuItem item2_2 = new JMenuItem("复制(C)");
        JMenuItem item2_3 = new JMenuItem("粘贴(V)");

        JRadioButtonMenuItem item3_1 = new JRadioButtonMenuItem("自动换行(W)",false);
        item3_1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                Object source = e.getSource();

                if(source == item3_1)
                    workArea.setLineWrap(true);    //自动换行
                else if(source != item3_1)
                    workArea.setLineWrap(false);
            }
        });

        //在菜单中添加菜单项
        file.add(item1_1);file.add(item1_2);file.add(item1_3);file.add(item1_4);file.add(item1_5);
        edit.add(item2_1);edit.add(item2_2);edit.add(item2_3);

    }//构造方法结束

    public static void main(String args[])
    {
        hi app = new hi();
        app.setSize(600, 400);
        app.setLocation(200,200);
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        app.setVisible(true);
    }
}
